package net.aiion.weave;

import java.util.Random;

import net.aiion.weave.asm.ClassVisitor;
import net.aiion.weave.asm.Label;
import net.aiion.weave.asm.MethodVisitor;
import net.aiion.weave.asm.Opcodes;

public class CardIfClass implements Opcodes
{
    ClassVisitor cw = null;
    MethodVisitor mv = null;

    public CardIfClass() {
    }

    public static int rnd() {
        return new Random().nextInt();
    }

    int a = rnd();
    int b = rnd();
    int c = rnd();
    int d = rnd();
    int e = rnd();
    int f = rnd();

    //
    // public void doSomething() {
    // if (a == 10) {
    // a = a + 11;
    // if (b == 12) {
    // b = b + 13;
    //
    // if (c == 14) {
    // c = c + 15;
    // }
    //
    // if (d == 16) {
    // d = d + 17;
    // }
    // }
    // if (e == 18) {
    // e = e + 19;
    // }
    // }
    //
    // if (f == 20) {
    // f = f + 21;
    // }
    // }
    //

    public void doSomething1() {
        Label l1 = new Label();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, "net/aiion/weave/CardClass", "a", "I");
        mv.visitIntInsn(BIPUSH, 10);
        mv.visitJumpInsn(IF_ICMPNE, l1);
        if (this.a == 10) {
            this.a = this.a + 11;

            Label l4 = new Label();
            mv.visitVarInsn(ALOAD, 0);
            mv.visitFieldInsn(GETFIELD, "net/aiion/weave/CardClass", "b", "I");
            mv.visitIntInsn(BIPUSH, 12);
            mv.visitJumpInsn(IF_ICMPNE, l4);
            if (this.b == 12) {
                this.b = this.b + 13;

                Label l7 = new Label();
                mv.visitVarInsn(ALOAD, 0);
                mv.visitFieldInsn(GETFIELD, "net/aiion/weave/CardClass", "c", "I");
                mv.visitIntInsn(BIPUSH, 14);
                mv.visitJumpInsn(IF_ICMPNE, l7);
                if (this.c == 14) {
                    this.c = this.c + 15;
                }
                mv.visitLabel(l7);
                mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);

                Label ld = new Label();
                mv.visitVarInsn(ALOAD, 0);
                mv.visitFieldInsn(GETFIELD, "net/aiion/weave/CardClass", "d", "I");
                mv.visitIntInsn(BIPUSH, 16);
                mv.visitJumpInsn(IF_ICMPNE, ld); // l4
                if (this.d == 16) {
                    this.d = this.d + 17;
                }
                mv.visitLabel(ld);
            }
            mv.visitLabel(l4);
            mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);

            Label le = new Label();
            mv.visitVarInsn(ALOAD, 0);
            mv.visitFieldInsn(GETFIELD, "net/aiion/weave/CardClass", "e", "I");
            mv.visitIntInsn(BIPUSH, 18);
            mv.visitJumpInsn(IF_ICMPNE, le); // l1
            if (this.e == 18) {
                this.e = this.e + 19;
            }
            mv.visitLabel(le);
        }
        mv.visitLabel(l1);
        mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);

        Label l11 = new Label();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, "net/aiion/weave/CardClass", "f", "I");
        mv.visitIntInsn(BIPUSH, 20);
        mv.visitJumpInsn(IF_ICMPNE, l11);
        if (this.f == 20) {
            this.f = this.f + 21;
        }
        mv.visitLabel(l11);
        mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);

        mv.visitInsn(RETURN);
    }

    public void doCompare() {
        Object o = new Object();
        if (o != null && !Boolean.FALSE.equals(o)) {
            System.out.println("Foo!");
        }

        Label l2 = new Label();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitJumpInsn(IFNULL, l2);
        mv.visitFieldInsn(GETSTATIC, "java/lang/Boolean", "FALSE", "Ljava/lang/Boolean;");
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Boolean", "equals", "(Ljava/lang/Object;)Z");
        mv.visitJumpInsn(IFNE, l2);

        mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
        mv.visitLdcInsn("Foo!");
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V");

        mv.visitLabel(l2);
        mv.visitLineNumber(126, l2);
        mv.visitFrame(Opcodes.F_APPEND, 1, new Object[] {
            "java/lang/Object"
        }, 0, null);

        mv.visitInsn(RETURN);
    }
}
